package com.lanou.myim.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.easemob.EMError;
import com.easemob.chat.EMChatManager;
import com.easemob.exceptions.EaseMobException;
import com.lanou.myim.R;

/**
 * Created by dllo on 16/3/1.
 */
public class RegisterActivity extends BaseActivity {
    private int error = 10;
    private String errorMsg;
    private EditText etUserName;
    private EditText etPassword;
    private EditText etconfirmPassword;
    private Button register;
    private String userName, password, confirmPassword;
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {
            error = message.arg1;
            switch (error) {
                case 0: {
                    error = 9;
                    Toast.makeText(getApplicationContext(), "网络异常，请检查网络！", Toast.LENGTH_SHORT).show();
                }
                break;
                case 1: {
                    error = 9;
                    Toast.makeText(getApplicationContext(), "用户已存在！", Toast.LENGTH_SHORT).show();
                }
                break;
                case 2: {
                    error = 9;
                    Toast.makeText(getApplicationContext(), "注册失败，无权限！", Toast.LENGTH_SHORT).show();
                }
                break;
                case 3: {
                    error = 9;
                    Toast.makeText(getApplicationContext(), "注册成功", Toast.LENGTH_SHORT).show();
                    EMChatManager.getInstance().logout();
                    Intent intent = new Intent(RegisterActivity.this, LogInActivity.class);
                    startActivity(intent);
                    finish();
                }
                break;
                case 4: {
                    error = 9;
                    Toast.makeText(getApplicationContext(), "注册失败: " + errorMsg, Toast.LENGTH_SHORT).show();
                }
                break;
            }


            return false;
        }
    });


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        etUserName = (EditText) findViewById(R.id.et_register_userName);
        etPassword = (EditText) findViewById(R.id.et_register_password);
        etconfirmPassword = (EditText) findViewById(R.id.et_register_confirmPassword);
        register = (Button) findViewById(R.id.btn_register_register);


        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                userName = String.valueOf(etUserName.getText());
                password = String.valueOf(etPassword.getText());
                confirmPassword = String.valueOf(etconfirmPassword.getText());
                if (userName.length() > 1) {


                    if (password.equals(confirmPassword)) {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                int errorCode = 0;
                                try {
                                    EMChatManager.getInstance().createAccountOnServer(userName, password);
                                } catch (EaseMobException e) {

                                    errorCode = e.getErrorCode();
                                    if (errorCode == EMError.NONETWORK_ERROR) {

                                        Message message = new Message();
                                        message.arg1 = 0;
                                        handler.sendMessage(message);
                                        // Toast.makeText(getApplicationContext(), "网络异常，请检查网络！", Toast.LENGTH_SHORT).show();
                                    } else if (errorCode == EMError.USER_ALREADY_EXISTS) {

                                        Message message = new Message();
                                        message.arg1 = 1;
                                        handler.sendMessage(message);
                                        // Toast.makeText(getApplicationContext(), "用户已存在！", Toast.LENGTH_SHORT).show();
                                    } else if (errorCode == EMError.UNAUTHORIZED) {

                                        Message message = new Message();
                                        message.arg1 = 2;
                                        handler.sendMessage(message);
                                        // Toast.makeText(getApplicationContext(), "注册失败，无权限！", Toast.LENGTH_SHORT).show();
                                    } else {

                                        Message message = new Message();
                                        message.arg1 = 4;
                                        Log.d("RegisterActivity", "e:" + e);
                                        handler.sendMessage(message);
                                        // Toast.makeText(getApplicationContext(), "注册失败: " + e.getMessage(), Toast.LENGTH_SHORT).show();
                                    }
                                }
                                if (errorCode == 0) {
                                    Message message = new Message();
                                    message.arg1 = 3;
                                    handler.sendMessage(message);
                                }
                            }
                        }).start();
                    } else {
                        Toast.makeText(getApplicationContext(), "输入的密码不一致", Toast.LENGTH_SHORT).show();

                    }
                } else {
                    Toast.makeText(getApplicationContext(), "用户名不能为空", Toast.LENGTH_SHORT).show();

                }


            }
        });
    }

}


